#include "game.h"
#include "ThrowableObj.h"
#include "ASprite.h"

ThrowableObj::ThrowableObj()
{
	SetObjectType(Object_ThrowableObj);	
}

void ThrowableObj::SaveData(CWriteFile* stream)
{
	GameObject::SaveData(stream);


}
void ThrowableObj::LoadData(CReadFile* stream)
{
	GameObject::LoadData(stream);
}

void ThrowableObj::Load(IAttributes* attr)
{
	
}



void ThrowableObj::Draw()
{
	//m_sprite->PaintFrame(0,m_position.X,m_position.Y);
	
	//g2DCommRI.tex = (TexInfo*)getTexMgr()->GetTex(0);
	//getGfx()->Flush2D();
	

	//getGfx()->SetColor(0xff0000ff);	
	//getGfx()->DrawRect(m_boundingbox->left,
	//	m_boundingbox->top,
	//	m_boundingbox->GetWidth(),
	//	m_boundingbox->GetHeight());

#if (SHOW_BOUND_RECT)
	getGfx()->SetColor(0xff00ff00);	
	getGfx()->DrawRect(m_boundingbox->left,
		m_boundingbox->top,
		m_boundingbox->GetWidth(),
		m_boundingbox->GetHeight());
#endif
}

void ThrowableObj::Update()
{
	GameObject::Update();
	//if(CanMove())
	//{		
	//	/*if(GetPosition().X < 120 ||
	//		GetPosition().Y > 500)
	//		SetEnabled(false);*/

	//	m_Speed.set(m_vx,m_vy);
	//	m_vy += 1;
	//}
}

bool ThrowableObj::ShouldCollideWith( GameObject* h )
{
	if(h && h->GetObjectType() == Object_BaseBot)
	{
		return true;
	}

	return false;
}

void ThrowableObj::Initialize()
{
	
}


void ThrowableObj::InitPhysics()
{	
	{
		b2BodyDef bodyDef;
		bodyDef.position.x = 0;
		bodyDef.position.y = 0;
		bodyDef.angle = 0;//m_boundingVolume.GetOrientation().z;
		ASSERT( !m_physicsBody );
		m_physicsBody = GetGameObjectManager()->GetPhysicsWorld().CreateBody( &bodyDef );
		ASSERT( m_physicsBody );
		if ( m_physicsBody )
		{
			b2PolygonDef shapeDef;
			
			shapeDef.vertexCount = 4;

			shapeDef.vertices[0].Set(-0.1f, 0.0f);
			shapeDef.vertices[1].Set(0.1f, 0.0f);
			shapeDef.vertices[2].Set(0.1f, 0.2f);
			shapeDef.vertices[3].Set(-0.1f, 0.2f);

			shapeDef.density = 1.0f;
			shapeDef.friction = 0.3f;
			//shapeDef.filter.categoryBits = 0x0004;

			m_physicsBody->CreateShape( &shapeDef );
			m_physicsBody->SetMassFromShapes();		

			m_physicsBody->SetUserData( this );	
		}
	}
}

void ThrowableObj::Update_PreCollision()
{

}
void ThrowableObj::Update_PostCollision()
{
	ResolveCollisions();

	//SetPosition(GetPosition() + m_displacement);
	//ReCalBoundingBox();

	
	//if(m_lastSpeed != m_Speed)
	//{
	//	//printf("\nNew Speed: %4f,%4f",m_Speed.X,m_Speed.Y);
	//	m_lastSpeed = m_Speed;
	//}

	for(int i = 0;i<m_collisionList.size();i++)
	{
		GameObject* h = m_collisionList[i].obj;

		//TRACE("Collision with: %s\n", h->GetName() );

		if ( h )
		{
			UnSpawn();
		}
	}

}


